package com.qys.livesMall.shop.controller.app;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qys.livesMall.common.result.R;
import com.qys.livesMall.framework.utils.SecurityUtils;
import com.qys.livesMall.shop.modal.dto.AddPurchaseCartDTO;
import com.qys.livesMall.shop.modal.dto.QueryPurchaseCartDTO;
import com.qys.livesMall.shop.modal.vo.PurchaseCartVO;
import com.qys.livesMall.shop.service.PurchaseCartService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 用户采购购物车(PurchaseCart)表控制层
 *
 * @author makejava
 * @since 2023-08-28 11:10:38
 */
@RestController
@RequestMapping("/app/purchaseCart")
@Tag(name = "App-采购购物车", description = "App-采购购物车")
public class PurchaseCartController {
    /**
     * 服务对象
     */
    @Resource
    private PurchaseCartService purchaseCartService;

    /**
     * 分页查询当前用户的采购购物车数据
     *
     * @param dto 查询实体
     * @return 所有数据
     */
    @PostMapping("/getAllPage")
    @Operation(summary = "查询当前用户的采购购物车数据")
    public R<IPage<PurchaseCartVO>> selectAll(@RequestBody QueryPurchaseCartDTO dto) {
        return R.ok(purchaseCartService.selectAll(SecurityUtils.getUserId(), dto));
    }


    /**
     * 添加商品至采购购物车
     *
     * @param dto 实体对象
     * @return 新增结果
     */
    @PostMapping("/add")
    @Operation(summary = "添加商品至采购购物车")
    public R<?> add(@RequestBody @Validated AddPurchaseCartDTO dto) {
        purchaseCartService.add(dto);
        return R.ok();
    }

    /**
     * 添加商品至采购购物车--批量
     *
     * @param dto 实体对象
     * @return 新增结果
     */
    @PostMapping("/addAll")
    @Operation(summary = "批量添加商品至采购购物车")
    public R<?> addAll(@RequestBody @Validated List<AddPurchaseCartDTO> dto) {
        for (AddPurchaseCartDTO cartDTO:dto) {
            purchaseCartService.add(cartDTO);
        }
        return R.ok();
    }


    /**
     * 删除用户的采购购物车中某一条商品
     *
     * @param idList 数据id
     * @return 删除结果
     */
    @PostMapping("/del")
    @Operation(summary = "删除用户的采购购物车中某一条或多条商品")
    public R<?> delete(@RequestBody List<Long> idList) {
        purchaseCartService.removePurchaseCart(idList);
        return R.ok();
    }
}

